from django.core.management.base import BaseCommand, CommandError
from apps.order.models import *
from apps.message.models import *
from apps.product.models import *
from apps.repo.models import *
from datetime import timedelta
import time
from django.db import transaction
import sys

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('mobile', type=str)

    def handle(self, *args, **options):
        mobile = options.get('mobile')

        user = User.objects.get(mobile=mobile)

        orders = Order.objects.filter(buyer_user_id=user.id, create_type=Order.CreateType.TAKE_OUT.name).order_by('-add_timestamp')
        sys.stdout.write('{n}单'.format(n=orders.count()))

        for order in orders:
            print('---------------------')
            print('OrderTime: %s OrderId: %s OrderNo: %s Amount: %s' % (order.get_add_time(), order.pk, order.no, Order.amount.__str__()))
            sub_orders = SubOrder.objects.filter(order=order)
            for so in sub_orders:
                gb = GoodsBlock.objects.get(pk=so.goods_block_id)
                print('SubOrderId: %s GoodsBlockId: %s GoodsBlockUniqueCode: %s' % (so.id, gb.id, gb.unique_code))
        print()